#ifndef ALG_DUMB2_HPP
#define ALG_DUMB2_HPP
#include <cmath>

using namespace std;

class Dumb2: public PathAlg
{
public:
    Dumb2(){}

    void init(Map &map)
    {
        unsigned deltaX, deltaY;
        deltaX = map.dX() - map.sX();
        deltaY = map.dY() - map.sY();
        if(deltaX == 0 && deltaY ==0)
        {
            _walkX = _walkY = 0;
        }
        if(deltaX > deltaY)
        {
            _walkX = abs(double(deltaX)/deltaY)*(deltaX < 0?-1:1);
            if(deltaY == 0)
            {
                _xTurn = true;
                _turns = false;
            }
            else
            {
                _xTurn = true;
                _turns = true;
                _walkY = deltaY<0?-1:1;
            }
        }
        else
        {
            _walkY = abs(double(deltaY)/deltaX)*(deltaY < 0?-1:1);
            if(deltaX == 0)
            {
                _xTurn = false;
                _turns = false;
            }
            else
            {
                _xTurn = false;
                _turns = true;
                _walkX = deltaX<0?-1:1;
            }
        }
        _turnSteps =0;
    }
    Result step(Map &map)
    {
        if(map.x() == map.dX() && map.y() == map.dY()) return ALG_SUCCEEDED;
        if(_xTurn)
        {
            if(_walkX > 0)
                map.walk(EAST);
            else map.walk(WEST);
            ++_turnSteps;
            if(_turns && _turnSteps == unsigned(abs(_walkX)))
            {
                _xTurn = false;
                _turnSteps =0;
            }
        }
        else
        {
            if(_walkY > 0)
                map.walk(SOUTH);
            else map.walk(NORTH);
            ++_turnSteps;
            if(_turns && _turnSteps == unsigned(abs(_walkY)))
            {
                _xTurn = true;
                _turnSteps =0;
            }
        }
        return ALG_PROCESSING;
    }
private:
    double _walkX, _walkY;
    unsigned _turnSteps;
    bool _turns, _xTurn;
};


#endif // ALG_DUMB2_HPP

